package com.guan.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guan.common.core.domain.TableDataInfo;
import com.guan.common.core.enums.ResultCode;
import com.guan.ojcommonsecurity.exception.ServiceException;
import com.guan.system.domain.question.Question;
import com.guan.system.domain.question.dto.QuestionAddDTO;
import com.guan.system.domain.question.dto.QuestionEditDTO;
import com.guan.system.domain.question.dto.QuestionQueryDTO;
import com.guan.system.domain.question.es.QuestionES;
import com.guan.system.domain.question.vo.QuestionDetailVO;
import com.guan.system.domain.question.vo.QuestionVO;
import com.guan.system.elasticsearch.IQuestionRepository;
import com.guan.system.manager.QuestionCacheManager;
import com.guan.system.mapper.QuestionMapper;
import com.guan.system.service.question.IQuestionService;
import jakarta.validation.constraints.AssertFalse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {


    private QuestionMapper questionMapper;

    private IQuestionRepository questionRepository;

    private QuestionCacheManager questionCacheManager;

    @Autowired
    public QuestionServiceImpl(QuestionMapper questionMapper , IQuestionRepository questionRepository, QuestionCacheManager questionCacheManager) {
        this.questionMapper = questionMapper;
        this.questionRepository = questionRepository;
        this.questionCacheManager = questionCacheManager;
    }

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        // 判重
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questionList)) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }

        Question question = new Question();
        BeanUtil.copyProperties(questionAddDTO, question);

        // 插入数据
        int insert = questionMapper.insert(question);
        if(insert <= 0) {
            return false;
        }
        // 将数据更新到ES
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question , questionES);
        questionRepository.save(questionES);
        // 添加缓存
        questionCacheManager.addCache(question.getQuestionId());
        return true;
    }

    @Override
    public QuestionDetailVO detail(@PathVariable long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        // 将model转换成dto
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question, questionDetailVO);
        //System.out.println(questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        System.out.println(questionEditDTO.getQuestionId());
        Question oldQuestion = questionMapper.selectById(questionEditDTO.getQuestionId());
        System.out.println(oldQuestion);
        if(oldQuestion == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldQuestion.setMainFunc(questionEditDTO.getMainFunc());
        // 将更新结果 刷新到ES
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(oldQuestion , questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(oldQuestion);
    }

    @Override
    public int delete(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(question.getQuestionId());
        return questionMapper.deleteById(questionId);
    }
}
